package unit6.replaceTempWithQuery;

/**
 * 以查询取代临时变量
 */
public class Example1 {

    private int quantity;
    private double itemPrice;

    /**
     * 例1
     * before refactoring
     */
    public double test1() {
        double basePrice = quantity * itemPrice;
        if (basePrice > 1000) {
            return basePrice * 0.95;
        } else {
            return basePrice * 0.98;
        }
    }

    /**
     * after refactoring
     */
    public double test1a() {
        if (getBasePrice() > 1000) {
            return getBasePrice() * 0.95;
        } else {
            return getBasePrice() * 0.98;
        }
    }

    private double getBasePrice() {
        return quantity * itemPrice;
    }


    /**
     * 例2
     * before refactoring
     */
    public double test2() {
        double basePrice = quantity * itemPrice;
        double discountFactor;
        if (basePrice > 1000) {
            discountFactor = 0.95;
        } else {
            discountFactor = 0.98;
        }
        return basePrice * discountFactor;
    }

    /**
     * after refactoring
     */
    public double test2a() {
        return getBasePrice() * getDiscountFactor();
    }

    private Double getDiscountFactor(){
        if (getBasePrice() > 1000) {
            return 0.95;
        } else {
            return 0.98;
        }
    }
}
